package com.icart.controller.personController;

import com.icart.context.HttpUserContext;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;

import com.icart.repositories.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

import java.util.List;


@Controller
@RequestMapping("/person")
public class MymessageController {

    @Autowired
    MessageRepository messageRepository;
    @Autowired
    HttpServletRequest request;


    /*我的信息*/
    @RequestMapping("/mymessage")
    public String mymessage(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*所有信息*/
        List list=messageRepository.createNativeQuery("select a.msgid,IF(a.sourceobj ='chn',(select b.chnname from bi_chn b where b.chnid=a.sourceobjid),'系统消息')as sourcename," +
                "a.msgtitle,a.msg,DATE_FORMAT(a.sendtime,'%Y-%m-%d')sendtime,a.readstat from bi_msg a where a.sendstat=1 and a.obj='cust' and a.readstat in(0,1) and a.objid="+userContext.getCustid());
        model.addAttribute("list",list);

        /*未读信息*/
        List list2=messageRepository.createNativeQuery("select a.msgid,IF(a.sourceobj ='chn',(select b.chnname from bi_chn b where b.chnid=a.sourceobjid),'系统消息')as sourcename," +
                "a.msgtitle,a.msg,DATE_FORMAT(a.sendtime,'%Y-%m-%d')sendtime from bi_msg a where a.sendstat=1 and a.obj='cust' and a.readstat=0 and a.objid="+userContext.getCustid());
        model.addAttribute("list2",list2);

        /*已读信息*/
        List list3=messageRepository.createNativeQuery("select a.msgid,IF(a.sourceobj ='chn',(select b.chnname from bi_chn b where b.chnid=a.sourceobjid),'系统消息')as sourcename," +
                "a.msgtitle,a.msg,DATE_FORMAT(a.sendtime,'%Y-%m-%d')sendtime from bi_msg a where a.sendstat=1 and a.obj='cust' and a.readstat=1 and a.objid="+userContext.getCustid());
        model.addAttribute("list3",list3);
        return "person/mymessage/mymessage";
    }

    /*读后修改状态*/
    @RequestMapping("/afterread")
    public String afterread(@RequestParam(name = "msgid",required = false) Long msgid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=messageRepository.update(msgid,userContext.getCustid().toString());
        if(flag==1){
            return "redirect:/person/mymessage";
        }else {
            return "redirect:/person/mymessage";
        }
    }

    /*删除信息*/
    @RequestMapping("/deletemessage")
    public String deletemessage(@RequestParam(name = "msgid",required = false) Long msgid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        Integer flag=messageRepository.delete(msgid);
        if(flag==1){
            return "redirect:/person/mymessage";
        }else {
            return "redirect:/person/mymessage";
        }
    }
}